package br.com.amadeus.android.dao;


import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import br.com.amadeus.android.entities.Course;
import br.com.amadeus.android.entities.Forum;
import br.com.amadeus.android.entities.Goal;
import br.com.amadeus.android.entities.Resource;

public class ResourceDAO {
	public static String[] columns = new String[] { ResourceDAO.ID,
			ResourceDAO.NAME, ResourceDAO.VALUE, ResourceDAO.EXTENSION, 
			ResourceDAO.COURSE_ID,
			ResourceDAO.FORUM_ID,
			ResourceDAO.AMADEUS_ID};

	public static final String ID = "_id";
	public static final String NAME = "name";
	public static final String VALUE = "value";
	public static final String COURSE_ID = "course_id";
	public static final String FORUM_ID = "forum_id";
	public static final String EXTENSION = "extension";
	public static final String AMADEUS_ID = "amadeus_id";
	
	private SQLiteDatabase db;

	private static ResourceDAO instance = new ResourceDAO();

	public static ResourceDAO getInstance(Context ctx) {

		if (instance.db == null || !instance.db.isOpen()) {
			instance.db = new DBHelper(ctx).getWritableDatabase();
		}
		return instance;
	}

	private ResourceDAO() {
	}

	public long save(Resource resource) {
		long id = resource.getId();
		if (id == 0) {
			return insert(resource);
		} else {
			return update(resource);
		}
	}

	private long insert(Resource resource) {
		long id_resource = -1;

		db.beginTransaction();
		try {
			
			ContentValues values = new ContentValues();
			values.put(ResourceDAO.NAME, resource.getName());
			values.put(ResourceDAO.VALUE, resource.getValue());
			values.put(ResourceDAO.COURSE_ID, resource.getCourse().getId());
			if (resource.getForum() != null) {
				values.put(ResourceDAO.FORUM_ID, resource.getForum().getId());
			}
			values.put(ResourceDAO.EXTENSION, resource.getExtension());
			values.put(ResourceDAO.AMADEUS_ID, resource.getAmadeus_id());

			id_resource = db.insert(DBHelper.TABLE_RESOURCE, null, values);
			
			if (id_resource != -1)
				db.setTransactionSuccessful();

		} finally {
			db.endTransaction();
		}

		return id_resource;
	}
	
	private long update(Resource resource) {
		boolean deuErro = false;
		db.beginTransaction();
		try {
			
			ContentValues values = new ContentValues();
			values.put(ResourceDAO.NAME, resource.getName());
			values.put(ResourceDAO.VALUE, resource.getValue());
			values.put(ResourceDAO.COURSE_ID, resource.getCourse().getId());
			if (resource.getForum() != null) {
				values.put(ResourceDAO.FORUM_ID, resource.getForum().getId());
			}
			values.put(ResourceDAO.EXTENSION, resource.getExtension());
			values.put(ResourceDAO.AMADEUS_ID, resource.getAmadeus_id());

			deuErro = db.update(DBHelper.TABLE_RESOURCE, values, ResourceDAO.ID
					+ " = ?", new String[] { String.valueOf(resource.getId()) }) == 0;

			if (!deuErro)
				db.setTransactionSuccessful();

		} finally {
			db.endTransaction();
		}
		return resource.getId();
	}
	
	public void delete(int id) {
		db.beginTransaction();
		try {
			db.delete(DBHelper.TABLE_RESOURCE, ResourceDAO.ID + "=?",
					new String[] { String.valueOf(id) });
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
	}
	
	public List<Resource> getListResources(){
		List<Resource> resourceList = new ArrayList<Resource>();
		Cursor c = db.query(DBHelper.TABLE_RESOURCE, columns, null,	null, null, null, null);
		if (c.moveToFirst()) {
			int id_id = c.getColumnIndex(ResourceDAO.ID);
			int id_name = c.getColumnIndex(ResourceDAO.NAME);
			int id_value = c.getColumnIndex(ResourceDAO.VALUE);
			int id_course = c.getColumnIndex(ResourceDAO.COURSE_ID);
			int id_forum_id = c.getColumnIndex(ResourceDAO.FORUM_ID);
			int id_ext = c.getColumnIndex(ResourceDAO.EXTENSION);
			int id_amadeus = c.getColumnIndex(ResourceDAO.AMADEUS_ID);

			do {

				int id = c.getInt(id_id);
				String name = c.getString(id_name);
				String value = c.getString(id_value);
				int course_id2 = c.getInt(id_course);
				Course course = new Course(course_id2);
				int forum_id = 0;
				forum_id = c.getInt(id_forum_id);
				Forum forum = new Forum(forum_id);
				String extension = c.getString(id_ext);
				int amadeus_id = c.getInt(id_amadeus);

				Resource resource = new Resource(id, name, value, course, forum, extension, amadeus_id);
				resourceList.add(resource);

			} while (c.moveToNext());
		}
		c.close();
		
		return resourceList;
	}
	
	public ArrayList<Resource> getListResourcesByCourse(int course_id){
		ArrayList<Resource> resourceList = new ArrayList<Resource>();
		Cursor c = db.query(DBHelper.TABLE_RESOURCE, columns, ResourceDAO.COURSE_ID + "=" + course_id, 
				null, null, null, null);
		if (c.moveToFirst()) {
			int id_id = c.getColumnIndex(ResourceDAO.ID);
			int id_name = c.getColumnIndex(ResourceDAO.NAME);
			int id_value = c.getColumnIndex(ResourceDAO.VALUE);
			int id_ext = c.getColumnIndex(ResourceDAO.EXTENSION);
			int id_course = c.getColumnIndex(ResourceDAO.COURSE_ID);
			int id_forum_id = c.getColumnIndex(ResourceDAO.FORUM_ID);
			int id_amadeus = c.getColumnIndex(ResourceDAO.AMADEUS_ID);
			
			do {
				
				int id = c.getInt(id_id);
				String name = c.getString(id_name);
				String value = c.getString(id_value);
				int course_id2 = c.getInt(id_course);
				Course course = new Course(course_id2);
				int forum_id = 0;
				forum_id = c.getInt(id_forum_id);
				Forum forum = new Forum(forum_id);
				String extension = c.getString(id_ext);
				int amadeus_id = c.getInt(id_amadeus);
				
				Resource resource = new  Resource(id, name, value, course, forum, extension, amadeus_id);
				resourceList.add(resource);
				
			} while (c.moveToNext());
		}
		c.close();	
		
		return resourceList;
	}
	
	public Resource getResource(int id) {
		Resource r = null;
		Cursor c = db.query(DBHelper.TABLE_RESOURCE, null, ResourceDAO.ID + "=" + id,
				null, null, null, null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			r = fillResource(c);
		}
		c.close();
		return r;
	}
	
	protected static Resource fillResource(Cursor c) {
		int id = c.getInt(c.getColumnIndex(ResourceDAO.ID));
		String name = c.getString(c.getColumnIndex(ResourceDAO.NAME));
		String value = c.getString(c.getColumnIndex(ResourceDAO.VALUE));
		Course course = new Course(c.getInt(c.getColumnIndex(ResourceDAO.COURSE_ID)));
		String extension = c.getString(c.getColumnIndex(ResourceDAO.EXTENSION));
		int amadeus_id = c.getInt(c.getColumnIndex(ResourceDAO.AMADEUS_ID));
		int forum_id = 0;
		forum_id = c.getInt(c.getColumnIndex(ResourceDAO.FORUM_ID));
		Forum forum = new Forum(forum_id);

		return new Resource(id, name, value, course, forum, extension, amadeus_id);
	}
	
	public Resource getResourceWithCourse(int id) {
		Cursor c = db.query(DBHelper.TABLE_RESOURCE, null, ResourceDAO.ID + "=" + id,
				null, null, null, null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			return fillResourceWithCourse(c);
		}
		return null;
	}

	protected Resource fillResourceWithCourse(Cursor c) {
		int id = c.getInt(c.getColumnIndex(ResourceDAO.ID));
		String name = c.getString(c.getColumnIndex(ResourceDAO.NAME));
		String value = c.getString(c.getColumnIndex(ResourceDAO.VALUE));
		Course course = new Course(c.getInt(c
				.getColumnIndex(ResourceDAO.COURSE_ID)));
		String extension = c.getString(c.getColumnIndex(ResourceDAO.EXTENSION));
		int amadeus_id = c.getInt(c.getColumnIndex(ResourceDAO.AMADEUS_ID));
		int forum_id = c.getInt(c.getColumnIndex(ResourceDAO.FORUM_ID));
		Forum forum = new Forum(forum_id);
		Resource resource = new Resource(id, name, value, course, forum, extension, amadeus_id);
		
		Cursor cc = db.query(DBHelper.TABLE_COURSE, null, "id = ?",
				new String[] { String.valueOf(resource.getCourse().getId()) },
				null, null, null);
		if (cc.getCount() > 0) {
			cc.moveToFirst();
			resource.setCourse(CourseDAO.fillCourse(cc));
		}

		return resource;
	}

	public ArrayList<Resource> getResourceByCourse(int course_id) {
		ArrayList<Resource> resourceList = new ArrayList<Resource>();
		
		Cursor c = db.query(DBHelper.TABLE_RESOURCE, null, ResourceDAO.COURSE_ID+"="+ course_id,
				null, null, null, null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			int i = 1;
			do {
				resourceList.add(fillResource(c));
			}while(c.moveToNext()); 
		}
		c.close();
		return resourceList;
	}
	
}
